%% produces Figures 3,4, O2

inde_const = find(lambda>0,1)-1;
ba         = A*b_vec_d/(A/nu-chi);
va_to_prof = A/(A/nu-chi);
inde_tresh = inde_bi(end);
inde_tresh2= inde_bi_uncon(end);



figure(2)
plot(ba(inde_bi_uncon),va_to_prof*dividend_uncon0(inde_bi_uncon),'--r','LineWidth',6),hold on
plot(ba(inde_bi),va_to_prof*max(dividend(inde_bi),0),'b','LineWidth',6),hold off
xline(ba(inde_const),'--k','FC binding: $b^*$','Interpreter','Latex','Fontsize',18)
xline(ba(inde_tresh),'--b','Bankruptcy treshold: $\bar{b}$','Interpreter','Latex','Fontsize',18,'LabelHorizontalAlignment','left')
xline(ba(inde_tresh2),'--r','Bankruptcy treshold w/o FC','Interpreter','Latex','Fontsize',18)
le=legend('Countefactual w/o FC','Baseline');
set(le,'Interpreter','Latex','Fontsize',16,'Location','southwest');
xlabel('Debt ratio $b$','Fontsize',24,'Interpreter','Latex')
ylabel('Dividend payout in units of $\mu$','Fontsize',24,'Interpreter','Latex')
settt,xlim([0 27])
grid on
str1="/figures/figure_d.pdf";
str_save1=append(root,str1);
print(gcf,'-dpdf',str_save1)


figure(3)
plot(ba(inde_bi(1:end-1)),va_to_prof*v_secon(inde_bi(1:end-1)),'b','LineWidth',6)
xline(ba(inde_const),'--k','FC binding: $b^*$','Interpreter','Latex','Fontsize',18)
xline(ba(inde_tresh),'--b','Bankruptcy treshold: $\bar{b}$','Interpreter','Latex','Fontsize',18,'LabelHorizontalAlignment','left')
xlabel('Debt ratio $b$','Fontsize',24,'Interpreter','Latex')
ylabel('Second derivative: $v^{\prime\,\prime}$','Fontsize',24,'Interpreter','Latex')
settt,xlim([0 27])
grid on
str1="/figures/figure_v2.pdf";
str_save1=append(root,str1);
print(gcf,'-dpdf',str_save1)

figure(4)
plot(ba(inde_bi),va_to_prof*v_prime(inde_bi),'b','LineWidth',6)
xline(ba(inde_const),'--k','FC binding: $b^*$','Interpreter','Latex','Fontsize',18)
xline(ba(inde_tresh),'--b','Bankruptcy treshold: $\bar{b}$','Interpreter','Latex','Fontsize',18,'LabelHorizontalAlignment','left')
xlabel('Debt ratio $b$','Fontsize',24,'Interpreter','Latex')
ylabel('First derivative: $v^{\prime}$','Fontsize',24,'Interpreter','Latex')
settt,xlim([0 27])
grid on
str1="/figures/figure_v1.pdf";
str_save1=append(root,str1);
print(gcf,'-dpdf',str_save1)

figure(5)
plot(ba(inde_bi),va_to_prof*v(inde_bi),'b','LineWidth',6),hold on
plot(ba(inde_bi),va_to_prof*v_tresh(inde_bi),'r--','LineWidth',6),hold off
xline(ba(inde_const),'--k','FC binding: $b^*$','Interpreter','Latex','Fontsize',18)
xline(ba(inde_tresh),'--b','Bankruptcy treshold: $\bar{b}$','Interpreter','Latex','Fontsize',18,'LabelHorizontalAlignment','left')
%xline(ba(inde_tresh2),'--r','Bankruptcy treshold')
grid on
le=legend('Equity value','Bankruptcy value');
set(le,'Interpreter','Latex','Fontsize',16,'Location','southWest');
%set(le,'Interpreter','Latex','Fontsize',16);
xlabel('Debt ratio $b$','Fontsize',24,'Interpreter','Latex')
ylabel('Equity value: $v$','Fontsize',24,'Interpreter','Latex')
settt,xlim([0 27])
grid on
str1="/figures/figure_v.pdf";
str_save1=append(root,str1);
print(gcf,'-dpdf',str_save1)

figure(7)
plot(ba(inde_bi_uncon),va_to_prof*ell_uncon0(inde_bi_uncon),'--r','LineWidth',6),hold on
plot(ba(inde_bi),va_to_prof*ell(inde_bi)','b','LineWidth',6),hold off
xline(ba(inde_const),'--k','FC binding: $b^*$','Interpreter','Latex','Fontsize',18)
xline(ba(inde_tresh),'--b','Bankruptcy treshold: $\bar{b}$','Interpreter','Latex','Fontsize',18,'LabelHorizontalAlignment','left')
xline(ba(inde_tresh2),'--r','Bankruptcy treshold w/o FC','Interpreter','Latex','Fontsize',18)
grid on
%le=legend('Countefactual w/o FC','Baseline');
%set(le,'Interpreter','Latex','Fontsize',16,'Location','northwest');
xlabel('Debt ratio $b$','Fontsize',24,'Interpreter','Latex')
ylabel('Debt issuance: $\ell$','Fontsize',24,'Interpreter','Latex')
settt,xlim([0 27])
grid on
str1="/figures/figure_ell.pdf";
str_save1=append(root,str1);
print(gcf,'-dpdf',str_save1)



figure(8)
plot(ba(inde_bi_uncon),x_uncon0(inde_bi_uncon),'--r','LineWidth',6),hold on
plot(ba(inde_bi),x(inde_bi)','b','LineWidth',6),hold off,
grid on
xline(ba(inde_const),'--k','FC binding: $b^*$','Interpreter','Latex','Fontsize',18)
xline(ba(inde_tresh),'--b','Bankruptcy treshold: $\bar{b}$','Interpreter','Latex','Fontsize',18,'LabelHorizontalAlignment','left')
xline(ba(inde_tresh2),'--r','Bankruptcy treshold w/o FC','Interpreter','Latex','Fontsize',18)
%le=legend('Countefactual w/o FC','Baseline');
%set(le,'Interpreter','Latex','Fontsize',16,'Location','southwest');
xlabel('Debt ratio $b$','Fontsize',24,'Interpreter','Latex')
ylabel('Debt price: $x$','Fontsize',24,'Interpreter','Latex')
settt,xlim([0 27])%,ylim([0 1])
grid on
str1="/figures/figure_x.pdf";
str_save1=append(root,str1);
print(gcf,'-dpdf',str_save1)


figure(9)
%plot(ba(inde_bi_uncon),zeros(length(inde_bi_uncon),1),'--r','LineWidth',6),hold on
plot(ba(inde_bi),va_to_prof*lambda(inde_bi)','b','LineWidth',6)
grid on
xline(ba(inde_const),'--k','FC binding: $b^*$','Interpreter','Latex','Fontsize',18)
xline(ba(inde_tresh),'--b','Bankruptcy treshold: $\bar{b}$','Interpreter','Latex','Fontsize',18,'LabelHorizontalAlignment','left')
%xline(ba(inde_tresh2),'--r','Bankruptcy treshold')
%le=legend('Countefactual w/o FC','Baseline');
%set(le,'Interpreter','Latex','Fontsize',16,'Location','southwest');
xlabel('Debt ratio $b$','Fontsize',24,'Interpreter','Latex')
ylabel('Lagrangian multiplier: $\lambda$','Fontsize',24,'Interpreter','Latex')
settt,xlim([0 27])
grid on
str1="/figures/figure_lam.pdf";
str_save1=append(root,str1);
print(gcf,'-dpdf',str_save1)

figure(10)
plot(ba(inde_bi_uncon),100*drift_I_uncon0(inde_bi_uncon),'--r','LineWidth',6),hold on
plot(ba(inde_bi),100*drift_I(inde_bi),'b','LineWidth',6),hold off
xline(ba(inde_const),'--k','FC binding: $b^*$','Interpreter','Latex','Fontsize',18,'LabelVerticalAlignment','middle')
xline(ba(inde_tresh),'--b','Bankruptcy treshold: $\bar{b}$','Interpreter','Latex','Fontsize',18,'LabelHorizontalAlignment','left')
xline(ba(inde_tresh2),'--r','Bankruptcy treshold w/o FC','Interpreter','Latex','Fontsize',18)
grid on
%le=legend('Countefactual w/o FC','Baseline');
%set(le,'Interpreter','Latex','Fontsize',16,'Location','southwest');
xlabel('Debt ratio $b$','Fontsize',24,'Interpreter','Latex')
ylabel('Investment rate: $\iota$, in \%','Fontsize',24,'Interpreter','Latex')
settt,xlim([0 27])
grid on
str1="/figures/figure_dz.pdf";
str_save1=append(root,str1);
print(gcf,'-dpdf',str_save1)





